package com.willian.andrappfw.ui;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.willian.andrappfw.R.styleable;

public class WRoundCornerCoverView  extends View {
    int topLeftRadius;
    int topRightRadius;
    int bottomLeftRadius;
    int bottomRightRadius = 0;
    @ColorInt
    int coverColor;

    public WRoundCornerCoverView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context, attrs);
    }

    public WRoundCornerCoverView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context, attrs);
    }

    public WRoundCornerCoverView(Context context) {
        super(context);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, styleable.RoundCornerCoverView);
        this.topRightRadius = this.topLeftRadius = this.bottomLeftRadius = this.bottomRightRadius = ta.getDimensionPixelSize(styleable.RoundCornerCoverView_radius, 0);
        this.topLeftRadius = ta.getDimensionPixelSize(styleable.RoundCornerCoverView_topLeftRadius, this.topLeftRadius);
        this.topRightRadius = ta.getDimensionPixelSize(styleable.RoundCornerCoverView_topRightRadius, this.topRightRadius);
        this.bottomLeftRadius = ta.getDimensionPixelSize(styleable.RoundCornerCoverView_bottomLeftRadius, this.bottomLeftRadius);
        this.bottomRightRadius = ta.getDimensionPixelSize(styleable.RoundCornerCoverView_bottomRightRadius, this.bottomRightRadius);
        this.coverColor = ta.getColor(styleable.RoundCornerCoverView_coverColor, -16777216);
        ta.recycle();
    }

    public void setRadius(int topLeftRadius, int topRightRadius, int bottomLeftRadius, int bottomRightRadius) {
        this.topLeftRadius = topLeftRadius;
        this.topRightRadius = topRightRadius;
        this.bottomLeftRadius = bottomLeftRadius;
        this.bottomRightRadius = bottomRightRadius;
    }

    public void setCoverColor(@ColorInt int color) {
        this.coverColor = color;
    }

    private Bitmap drawSector(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(1);
        p.setColor(-13244);
        c.drawArc(new RectF(0.0F, 0.0F, (float)(this.topLeftRadius * 2), (float)(this.topLeftRadius * 2)), 180.0F, 90.0F, true, p);
        c.drawArc(new RectF(0.0F, (float)(this.getHeight() - this.bottomLeftRadius * 2), (float)(this.bottomLeftRadius * 2), (float)this.getHeight()), 90.0F, 90.0F, true, p);
        c.drawArc(new RectF((float)(this.getWidth() - this.topRightRadius * 2), 0.0F, (float)this.getWidth(), (float)(this.topRightRadius * 2)), 270.0F, 90.0F, true, p);
        c.drawArc(new RectF((float)(this.getWidth() - this.bottomRightRadius * 2), (float)(this.getHeight() - this.bottomRightRadius * 2), (float)this.getWidth(), (float)this.getHeight()), 0.0F, 90.0F, true, p);
        return bm;
    }

    private Bitmap drawRect(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(1);
        p.setColor(this.coverColor);
        c.drawRect(new RectF(0.0F, 0.0F, (float)this.topLeftRadius, (float)this.topLeftRadius), p);
        c.drawRect(new RectF(0.0F, (float)(this.getHeight() - this.bottomLeftRadius), (float)this.bottomLeftRadius, (float)this.getHeight()), p);
        c.drawRect(new RectF((float)(this.getWidth() - this.topRightRadius), 0.0F, (float)this.getWidth(), (float)this.topRightRadius), p);
        c.drawRect(new RectF((float)(this.getWidth() - this.bottomRightRadius), (float)(this.getHeight() - this.bottomRightRadius), (float)this.getWidth(), (float)this.getHeight()), p);
        return bm;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setFilterBitmap(false);
        paint.setStyle(Style.FILL);
        int sc = canvas.saveLayer(0.0F, 0.0F, (float)this.getWidth(), (float)this.getHeight(), (Paint)null);
        canvas.drawBitmap(this.drawSector(this.getWidth(), this.getHeight()), 0.0F, 0.0F, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
        canvas.drawBitmap(this.drawRect(this.getWidth(), this.getHeight()), 0.0F, 0.0F, paint);
        paint.setXfermode((Xfermode)null);
        canvas.restoreToCount(sc);
    }
}
